package com.bandi.tharshesnows.persistence;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.bandi.tharshesnows.model.County;
import com.bandi.tharshesnows.model.Location;
import com.bandi.tharshesnows.persistence.Metadata.CountyMetadata;

public class CountyDAO {

  private Database database;

  CountyDAO(Database database) {
    super();
    this.database = database;
  }

  public County create(County county) throws DAOException {
    try {
      SQLiteDatabase db = database.open();
      ContentValues values = new ContentValues();
      values.put(CountyMetadata.NAME, county.getName());
      values.put(CountyMetadata.STARRED, county.isStarred() ? 1 : 0);
      if (county.getLocation() != null) {
        values.put(CountyMetadata.LATITUDE, "" + county.getLocation().getLatitude());
        values.put(CountyMetadata.LONGITUDE, "" + county.getLocation().getLongitude());
        values.put(CountyMetadata.MAP_IMG, county.getLocation().getStaticMapImagePath());
      }
      county.set_id(db.insert(CountyMetadata.TABLE_NAME, null, values));
      return county;
    }
    catch (Throwable t) {
      throw new DAOException("Failed to create county.", t);
    }
    finally {
      database.close();
    }
  }

  public County get(long countyId) throws DAOException {
    try {
      SQLiteDatabase db = database.open();
      Cursor cursor = db.query(CountyMetadata.TABLE_NAME, CountyMetadata.COLUMNS,
          CountyMetadata._ID + " = " + countyId, null, null, null, null);

      if (cursor.moveToFirst()) {
        return map(cursor);
      }
      else {
        return null;
      }
    }
    catch (Throwable t) {
      throw new DAOException("Failed to get county.", t);
    }
    finally {
      database.close();
    }
  }

  public County get(String name) throws DAOException {
    try {
      SQLiteDatabase db = database.open();
      Cursor cursor = db.query(CountyMetadata.TABLE_NAME, CountyMetadata.COLUMNS, CountyMetadata.NAME + " = '" + name
          + "'", null, null, null, null);
      if (cursor.moveToFirst()) {
        return map(cursor);
      }
      else {
        return null;
      }
    }
    catch (Throwable t) {
      throw new DAOException("Failed to get county.", t);
    }
    finally {
      database.close();
    }
  }

  public List<County> getAll() throws DAOException {
    try {
      SQLiteDatabase db = database.open();
      List<County> counties = new ArrayList<County>();
      Cursor cursor = db.query(CountyMetadata.TABLE_NAME, CountyMetadata.COLUMNS, null, null, null, null, null);
      cursor.moveToFirst();
      if (cursor.moveToFirst()) {
        do {
          counties.add(map(cursor));
        } while (cursor.moveToNext());
      }
      return counties;
    }
    catch (Throwable t) {
      throw new DAOException("Failed to get counties.", t);
    }
    finally {
      database.close();
    }
  }

  private County map(Cursor cursor) {
    County county = new County();
    county.set_id(cursor.getLong(cursor.getColumnIndex(CountyMetadata._ID)));
    county.setName(cursor.getString(cursor.getColumnIndex(CountyMetadata.NAME)));
    county.setStarred(cursor.getInt(cursor.getColumnIndex(CountyMetadata.STARRED)) == 1 ? true : false);
    String lat = cursor.getString(cursor.getColumnIndex(CountyMetadata.LATITUDE));
    String lon = cursor.getString(cursor.getColumnIndex(CountyMetadata.LONGITUDE));
    if (lat != null && lon != null && lat.length() > 0 && lon.length() > 0) {
      county.setLocation(new Location(Double.parseDouble(lat), Double.parseDouble(lon)));
      county.getLocation().setStaticMapImagePath(cursor.getString(cursor.getColumnIndex(CountyMetadata.MAP_IMG)));
    }
    return county;
  }

}
